﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

using System.Xml.Serialization;
using System.ComponentModel;

namespace XEvents.Library
{
    [Serializable]
    public class Event : XEObjectBase
    {
        #region Public Properties


        public override bool Equals(object obj)
        {
            if (obj == null) return false;

            if (this.GetType() != obj.GetType()) return false;

            // safe because of the GetType check
            Event evt = (Event)obj;

            // use this pattern to compare reference members
            if (!Object.Equals(Name, evt.Name)) return false;

            // use this pattern to compare value members
            if (!Package.Equals(evt.Package)) return false;

            return true;
        } 

        /// <summary>
        /// Columns for this Event
        /// </summary>
        [CategoryAttribute("Event Information"), DescriptionAttribute("Column Collection for this Event"), ReadOnlyAttribute(true)]
        public ColumnCollection Columns
        {
            get
            {
                return new ColumnCollection(from c in Server.MetaData.object_columns
                       where c.object_name == this.name
                       && c.object_package_guid == this.package_guid
                       && c.column_type != "readonly"
                       // TODO:  add additional filtering here....
                       select new Column(c, this.Server));
            }
        }

        /// <summary>
        /// ETW Channel for this Event
        /// </summary>
        [CategoryAttribute("Event Information"), DescriptionAttribute("ETW Channel for this Event"), ReadOnlyAttribute(true)]
        [XmlAttribute("Channel")]
        public string Channel
        {
            get
            {
                int value = Convert.ToInt32((from c in Server.MetaData.object_columns
                                where c.name == "CHANNEL" && c.object_name == this.name
                                select c.column_value).ElementAtOrDefault(0));

                                //from mv in Server.MetaData.map_values

                string chan = (from mv in Server.MetaData.map_values
                              where mv.name == "etw_channel"
                              //&& this.package_guid == mv.object_package_guid
                              && value == mv.map_key
                              select mv.map_value).ElementAtOrDefault(0);
                return chan;// (Channel)Enum.Parse(typeof(Channel), value);
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        /// <summary>
        /// ETW Keyword for this Event
        /// </summary>
        [CategoryAttribute("Event Information"), DescriptionAttribute("ETW Keyword for this Event"), ReadOnlyAttribute(true)]
        [XmlAttribute("Keyword")]
        public string Keyword
        {
            get
            {
                string value = (from c in Server.MetaData.object_columns
                                where c.name == "KEYWORD" && c.object_name == this.name
                                from mv in Server.MetaData.map_values
                                where mv.name == "keyword_map" 
                                && c.object_package_guid == mv.object_package_guid 
                                && c.column_value == mv.map_key.ToString()
                                select mv.map_value).ElementAtOrDefault(0);
                return value;// (Keyword)Enum.Parse(typeof(Keyword), value ?? "0");
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        #endregion

        #region Constructor
        internal Event(dm_xe_object obj, SqlServer server):base(obj, server)
        { }

        private Event()
            : base(null)
        { }
        #endregion
    }
}
